﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class api_script_gameList : ScriptClass
{
    gameList bll = new gameList();
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request["action"];
        switch (action)
        {
            case "getGamneList": // 获取游戏列表
                getGamneList();
                break;
            //case "checkedExchangeInfo": // 检测兑换信息
            //    checkedExchangeInfo();
            //    break;
            //case "exchange": // 兑换
            //    exchange();
            //    break;
            case "searchRecord": // 查询兑换记录
                searchRecord();
                break;
            default:
                ResponseData rd = new ResponseData();
                rd.code = 0;
                rd.data = null;
                rd.message = "读写参数错误";
                Json(rd);
                break;
        }
    }

    private void searchRecord()
    {
        ResponseData rd = new ResponseData();
        try
        {
            string code = Request["code"]; //code
            if (code == null)
            {
                Response.Write("异常：缺少参数！");
                return;
            }
            rechargeRecord bllLog = new rechargeRecord();
            Hashtable h = new Hashtable();
            h["code"] = code;
            Hashtable res = bllLog.GetModel(h);
            if(res.Count > 0)
            {
                res.Remove("id");
            }
            rd.code = 1;
            rd.message = res.Count > 0 ? "查询成功" : "查询失败 无对应记录";
            rd.data = res;
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

    private void exchange()
    {
        ResponseData rd = new ResponseData();
        try
        {
            ArrayList list = bll.GetList();
            if (list.Count > 0)
            {
                rd.code = 1;
                rd.data = list;
                rd.message = "获取成功";
            }
            else
            {
                rd.code = -1;
                rd.message = "获取游戏数据失败";
                rd.data = null;
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

    private void getGamneList()
    {
        ResponseData rd = new ResponseData();
        try
        {
            ArrayList list = bll.GetList();
            if (list.Count > 0)
            {
                rd.code = 1;
                rd.data = list;
                rd.message = "获取成功";
            }
            else
            {
                rd.code = -1;
                rd.message = "获取游戏数据失败";
                rd.data = null;
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

    private void getRowContent()
    {
        ResponseData rd = new ResponseData();
        try
        {
            string onlyCode = Request["onlyCode"]; //唯一编码
            string code = Request["code"]; //code

            if (onlyCode == null || code == null)
            {
                Response.Write("异常：缺少参数！请检查 唯一编码：" + onlyCode + " 设备编号：" + code);
                return;
            }

            //检测账号合法性
            MemberUser user = new MemberUser();
            Hashtable hp_user = new Hashtable();
            hp_user["onlyCode"] = onlyCode;
            Hashtable h_user = user.GetModel(hp_user);
            MethodRes mr = TestNum(h_user);
            if (mr.code == -1)
            {
                Json(mr);
                return;
            }

            string sql = "select * from  deviceList where UserNum = @UserNum and code = @code";
            Hashtable h_sel = new Hashtable();
            h_sel["UserNum"] = h_user["loginNum"];
            h_sel["code"] = code;
            DataTable dt = new SQLHelper("getnum").ExcuteQuery(sql, h_sel);
            if (dt.Rows.Count > 0)
            {
                rd.code = 1;
                rd.data = dt.Rows[0].Table;
                rd.message = "获取成功";
            }
            else
            {
                rd.code = -1;
                rd.message = "获取失败，无此数据！";
                rd.data = null;
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

    private void setCellContent()
    {
        ResponseData rd = new ResponseData();
        try
        {
            string onlyCode = Request["onlyCode"]; //唯一编码
            string code = Request["code"]; //code
            string lieName = Request["lieName"]; //列名
            string updContent = Request["updContent"]; //修改内容

            if (onlyCode == null || code == null || lieName == null || updContent == null)
            {
                Response.Write("异常：缺少参数！请检查 唯一编码：" + onlyCode + " 设备编号：" + code + " 列名：" + lieName + " 修改内容：" + updContent);
                return;
            }

            //检测账号合法性
            MemberUser user = new MemberUser();
            Hashtable hp_user = new Hashtable();
            hp_user["onlyCode"] = onlyCode;
            Hashtable h_user = user.GetModel(hp_user);
            MethodRes mr = TestNum(h_user);
            if (mr.code == -1)
            {
                Json(mr);
                return;
            }
            string updateField = "";
            switch (lieName)
            {
                case "设备id":
                    updateField = "id1";
                    break;
                case "店铺复购间隔":
                    updateField = "id2";
                    break;
                case "app版本号":
                    updateField = "appVersion";
                    break;
                case "淘宝账号数":
                    updateField = "tbAccountCount";
                    break;
                case "抖音账号数":
                    updateField = "dyAccountCount";
                    break;
                default:
                    break;
            }
            string sql = "update deviceList set " + updateField + " = @updContent,updateTime = CONVERT(varchar(100), GETDATE(), 120) where UserNum = @UserNum and code = @code";
            Hashtable h_sel = new Hashtable();
            h_sel["UserNum"] = h_user["loginNum"];
            h_sel["code"] = code;
            h_sel["updContent"] = updContent;
            int ii = new SQLHelper("getnum").Execute(sql, h_sel);
            if (ii > 0)
            {
                rd.code = 1;
                rd.message = "修改设备：" + code + " ,列：" + lieName + "的数据成功";
            }
            else
            {
                rd.code = -1;
                rd.message = "修改失败，无此数据！";
            }

        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

}